package com.grom.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.grom.dao.SecurityDao;
import com.grom.po.Security;
import com.grom.util.Constants;

public interface SecurityService {
	SecurityService instance = new SecurityService() {
		public List<Security> getAllSecurity() {
			return SecurityDao.instance.getAllSecuritys();
		}

		public void add(Security security) {
			SecurityDao.instance.add(security);
		}

		public void update(Security security) {
			SecurityDao.instance.update(security);
		}

		public void delete(Security security) {
			SecurityDao.instance.delete(security);
		}

		public List<Security> selectByCondition(String sql) {
			return SecurityDao.instance.selectByCondition(sql);
		}

		public Security getSecurityById(String id) {
			return SecurityDao.instance.getSecurityById(id);
		}

		public List<Security> getAllRootSecurity() {
			return SecurityDao.instance.getAllRootSecurity();
		}

		public List<Security> getAllRootSecurityByUser(String userName) {
			if (userName == "ADMIN") {
				return getAllRootSecurity();
			}
			return null;
		}

		public List<Security> getAllParentSecurity() {
			String sql = "select * from security where parentId='"
					+ Constants.MODULE_ROOT
					+ "' or parentid IN (SELECT id from security where parentID='"
					+ Constants.MODULE_ROOT + "')";
			return SecurityDao.instance.selectByCondition(sql);
		}

		public Map<Security, List<Security>> getAllSecurityByParentId(
				String parentID) {
			Map<Security, List<Security>> result = new HashMap<Security, List<Security>>();
			List<Security> firstChildren = getChildSecurityById(parentID);
			for (Security security : firstChildren) {
				List<Security> secondChildren = getChildSecurityById(security
						.getID());
				result.put(security, secondChildren);
			}
			return result;
		}

		public List<Security> getChildSecurityById(String id) {
			String sql = "select * from security where parentid='" + id + "'";
			return selectByCondition(sql);
		}

		public Security getSecurityByAction(String action) {
			String sql = "select * from security where actionurl='" + action
					+ "'";
			List<Security> result = selectByCondition(sql);
			return result.get(0);
		}

		public List<Security> getAllSecurityRelToDep(String depId) {
			String sql = "select * from security where id in " +
					"(select securityId from relationsectodep where departmentId = '"+depId+"')";
			List<Security> result = selectByCondition(sql);
			return result;
		}
	};

	List<Security> getAllSecurity();

	List<Security> getAllRootSecurity();

	List<Security> getAllSecurityRelToDep(String depId);

	List<Security> getAllParentSecurity();

	List<Security> getChildSecurityById(String id);

	Map<Security, List<Security>> getAllSecurityByParentId(String parentID);

	List<Security> getAllRootSecurityByUser(String userName);

	Security getSecurityById(String id);

	Security getSecurityByAction(String action);

	void add(Security security);

	void update(Security security);

	void delete(Security security);

	List<Security> selectByCondition(String sql);
}
